package com.obas.components {

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import mx.events.FlexEvent;
	import mx.events.RSLEvent;
	import mx.preloaders.DownloadProgressBar;

	public class Preloader extends DownloadProgressBar {

      private var _progressText:TextField;
      private var _textFormat:TextFormat;
      private var _loadingSpinner:MovieClip;
      
      private var _totalBytes:Number;
      private var _loadedBytes:Number;

      public function Preloader() {
         super();
      }

      public override function set preloader(preloader:Sprite):void {
         _progressText = new TextField();

         _textFormat = new TextFormat();
         _textFormat.font = "Arial";
         _textFormat.size = 20;

         this.addChild( _progressText );
         manageLayout();

         // runtime shared library
         preloader.addEventListener( RSLEvent.RSL_PROGRESS, onRSLDownloadProgress );
         preloader.addEventListener( RSLEvent.RSL_COMPLETE, onRSLDownloadComplete );
         preloader.addEventListener( RSLEvent.RSL_ERROR, onRSLError );

         // application
         preloader.addEventListener( ProgressEvent.PROGRESS, onSWFDownloadProgress );
         preloader.addEventListener( Event.COMPLETE, onSWFDownloadComplete );

         preloader.addEventListener( FlexEvent.INIT_PROGRESS, onFlexInitProgress );
         preloader.addEventListener( FlexEvent.INIT_COMPLETE, onFlexInitComplete );

      }

      private function manageLayout():void {
         var w:int = stageWidth
         var h:int = stageHeight;

         _progressText.width = 400;

         _progressText.x = w/2 - 150;
         _progressText.y = h/2 - 50;

      }

      private function updateProgress():void {
      }

      protected function onRSLDownloadProgress( e:RSLEvent ):void {
         if ( e.rslIndex && e.rslTotal ) {
            if( _progressText == null ) {
               _progressText = new TextField();
               this.addChild( _progressText );
            }
            
            _progressText.text = "正在加载  " + e.rslIndex + "/" + e.rslTotal;
            _progressText.setTextFormat( _textFormat );
         }
      }

      private function onRSLDownloadComplete( event:RSLEvent ):void {
         trace("onRSLDownloadComplete: 100% - bytes total: " + event.bytesTotal);
      }

      private function onRSLError( event:RSLEvent ):void {
         trace("onRSLError: " + event.errorText + " - " + event.url);
      }

      protected function onSWFDownloadProgress(e: ProgressEvent):void {
         e.bytesLoaded;
         e.bytesTotal;
      }

      private function onSWFDownloadComplete( event:Event ):void {
         trace("onSWFDownloadComplete: 100%");
      }

      private function onFlexInitProgress( event:FlexEvent ):void {
         trace("onFlexInitProgress: Initializing...");
         try {
         }
         catch (e:Error) {
         }
      }

      private function onFlexInitComplete( event:FlexEvent ):void {
         trace("onFlexInitComplete");
         onDoneAnimating();
      }

      private function onDoneAnimating():void {
         trace("onDoneAnimating");
         dispatchEvent( new Event( Event.COMPLETE ) );
      }
	}
}